package com.example.shirotest.study;

public class TestValidator {

    public static void main(String[] args) {
        //1、创建节点
        HeroNode hero1 = new HeroNode(1,"shunwukong","swk");
        HeroNode hero2 = new HeroNode(2,"tangsheng","ts");
        HeroNode hero3 = new HeroNode(3,"zhubajie","zbj");
        HeroNode hero4 = new HeroNode(4,"shawujing","swj");

        //2、创建链表管理器
        DoubleLinkedList linkedList = new DoubleLinkedList();

        //3、添加节点
        linkedList.add(hero1);
        linkedList.add(hero4);
        linkedList.add(hero2);
        linkedList.add(hero3);

        //5、修改节点
        HeroNode hero5 = new HeroNode(1,"qtds","bmw");
        linkedList.update(hero5);

        //6、删除节点
        linkedList.del(3);

        //8、显示链表内容
        linkedList.list();
    }

}

//双向链表管理器
class DoubleLinkedList{
    //先初始化一个头节点，头节点不要动，不存放具体的数据
    private HeroNode head = new HeroNode(0,"","");

    //遍历链表
    public void list(){
        if(head.next == null){
            System.out.println("LinkedList is empty");
            return;
        }

        HeroNode temp = head.next;
        while (true){
            if(temp == null){
                break;
            }
            //输出节点
            System.out.println(temp);
            temp = temp.next;
        }
    }

    //添加节点到单向链表
    public void add(HeroNode heroNode){
        HeroNode temp = head;
        //遍历链表
        while (true){
            //找到链表的最后
            if(temp.next == null){
                break;
            }
            //如果没有找到，将temp后移
            temp = temp.next;
        }

        //将最后这个节点的next指向新的节点
        temp.next = heroNode;
        heroNode.prve = temp;
    }

    //修改
    public void update(HeroNode newHeroNode){
        if(head.next == null){
            System.out.println("The LinkedList is empty");
            return;
        }

        HeroNode temp = head.next;
        boolean flag = false;
        while (true){
            if(temp == null){
                break;
            }

            if(temp.no == newHeroNode.no){
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if(flag){
            temp.name = newHeroNode.name;
            temp.nickname = newHeroNode.nickname;
        }else {
            System.out.printf("Can't found data of No %d \n",newHeroNode.no);
        }

    }

    //删除节点
    public void del(int no){
        HeroNode temp = head.next;
        boolean flag = false;
        while (true){
            if(temp == null){
                break;
            }
            if(temp.no == no){
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if(flag){
            temp.prve.next = temp.next;
            if(temp.next != null){
                temp.next.prve = temp.prve;
            }
        }else{
            System.out.printf("The No %d not found",no);
        }
    }




}


class HeroNode{
    public int no;
    public String name;
    public String nickname;
    public HeroNode next; //指向下一个节点
    public HeroNode prve; //指向上一个节点

    public HeroNode(int no,String name,String nickname){
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}